package com.huawei123.hasee.cniao5store.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.PermissionChecker;
import android.view.View;
import android.widget.Button;

import com.huawei123.hasee.cniao5store.R;
import com.huawei123.hasee.cniao5store.base.BaseActivity;
import com.huawei123.hasee.cniao5store.mvp.view.acyivity.HomeActivity;
import com.huawei123.hasee.cniao5store.utils.LogUtils;

import butterknife.BindView;

/**
 * 引导页
 */
public class SplashActivity extends BaseActivity {
    @BindView(R.id.mBT_enter_button)
    Button mBT_enter_button;

    private SharedPreferences sharedPreferences = null;


    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_splash);
    }


    @Override
    protected void initView() {
        initData();
    }

    protected void initData() {
        /*判定是否登录过*/
        sharedPreferences = this.getSharedPreferences(String.valueOf(R.string.project_name), Context.MODE_PRIVATE);
        if (sharedPreferences.getBoolean("isFirst", true)) {
            /*是首次*/
        } else {
            /*不是首次*/
            this.startActivity(new Intent(getApplicationContext(), HomeActivity.class));
            finish();
        }
        /*请求权限*/
        mBT_enter_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sharedPreferences.edit().putBoolean("isFirst", false).commit();
                initPermission();
            }
        });

    }

    /**
     * 6.0以后请求SD卡权限
     */
    private void initPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            /*判定是否有读取SD卡权限*/
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
                    PermissionChecker.PERMISSION_GRANTED) {
                this.startActivity(new Intent(getApplicationContext(), HomeActivity.class));
                finish();
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
            grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 0) {
            if (grantResults[0] == PermissionChecker.PERMISSION_GRANTED) {
                LogUtils.showToastShort("授权SD卡成功");
            } else {
                LogUtils.showToastShort("没有授权SD卡，可能会影响应用的使用");
            }
        }
        this.startActivity(new Intent(getApplicationContext(), HomeActivity.class));
        finish();
    }
}
